﻿using System;
using System.Collections.Generic;

namespace SBPweb.MediaCenter.Common.Communication
{
    public abstract class MessageQueueBase<T> : Queue<T>, IMessageQueue where T : IMessage
    {

        #region Constructor logic

        protected MessageQueueBase()
        {

        }

        #endregion

        #region Event handlers

        public event EventHandler<MessageEventArgs> Enqueued;

        public event EventHandler<MessageEventArgs> Dequeued;

        public event EventHandler<MessageEventArgs> Peeked;

        #endregion

        #region Methods

        public void Enqueue(IMessage message)
        {
            base.Enqueue((T)message);
            if (Enqueued != null)
            {
                Enqueued(this, new MessageEventArgs(message));
            }
        }

        public new IMessage Dequeue()
        {
            IMessage ret = (T)base.Dequeue();
            if (Dequeued != null)
            {
                Dequeued(this, new MessageEventArgs(ret));
            }
            return ret;
        }

        public new IMessage Peek()
        {
            IMessage ret = (T)base.Peek();
            if (Peeked != null)
            {
                Peeked(this, new MessageEventArgs(ret));
            }
            return ret;
        }

        #endregion

    }
}